<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <!-- <el-form-item label="所属部门" prop="deptId">
        <treeselect v-model="queryParams.deptId" :options="deptOptions" :show-count="true" placeholder="管理部门"
          style="width: 480px" size="small" />
      </el-form-item> -->
      <el-form-item label="仓库名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入仓库名称" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="仓管员" prop="userId">
        <el-select v-model="queryParams.userId" placeholder="请选择仓管员" style="width: 180px">
          <el-option v-for="item in userOptions" :key="item.userId" :label="item.nickName" :value="item.userId" />
        </el-select>
      </el-form-item> -->

      <!-- <el-form-item prop="provinceCode">
        <el-select v-model="queryParams.provinceCode" @change="getAreaList($event, 2)" filterable placeholder="省">
          <el-option v-for="item in provinceOptions" :key="item.areaId" :label="item.areaName" :value="item.areaId"
            :disabled="item.status == 1"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item prop="cityCode" v-if="queryParams.provinceCode">
        <el-select v-model="queryParams.cityCode" @change="getAreaList($event, 3)" filterable placeholder="市">
          <el-option v-for="item in cityOptions" :key="item.areaId" :label="item.areaName" :value="item.areaId"
            :disabled="item.status == 1"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="区" prop="areaCode" v-if="queryParams.cityCode">
        <el-select v-model="queryParams.areaCode" filterable placeholder="区">
          <el-option v-for="item in areaOptions" :key="item.areaId" :label="item.areaName" :value="item.areaId"
            :disabled="item.status == 1"></el-option>
        </el-select>
      </el-form-item> -->


      <!-- <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option v-for="dict in dict.type.sys_common_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item> -->
      <!--
      <el-form-item label="删除标识" prop="delFlag">
        <el-select v-model="queryParams.delFlag" placeholder="请选择删除标识" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_del_flag"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['wms:store:add']">新增</el-button>
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['wms:store:edit']">修改</el-button>
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['wms:store:remove']">删除</el-button>
        <el-button type="info" plain icon="el-icon-sort" size="mini" @click="toggleExpandAll">展开/折叠</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['wms:store:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['wms:store:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['wms:store:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['wms:store:export']">导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-sort" size="mini" @click="toggleExpandAll">展开/折叠</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-if="refreshTable" v-loading="loading" :data="storeList" row-key="id" border
      :default-expand-all="isExpandAll" :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="仓库ID" align="center" prop="id" v-if="true"/> -->
      <!-- <el-table-column label="仓库编码" align="center" prop="storeCode" fixed="left"/> -->
      <el-table-column label="仓库名称" prop="name" width="90"/>
      <!-- <el-table-column label="所属部门" prop="deptName" min-width="260"/> -->
      <el-table-column label="仓管员" prop="nickName" width="160"/>
      <el-table-column label="联系电话" prop="tel" width="110"/>
      <!-- <el-table-column label="省份" align="center" prop="province" />
      <el-table-column label="市" align="center" prop="city" />
      <el-table-column label="区" align="center" prop="area" /> -->
      <!-- <el-table-column label="省代码" align="center" prop="provinceCode" />
      <el-table-column label="市代码" align="center" prop="cityCode" />
      <el-table-column label="区县代码" align="center" prop="areaCode" /> -->
      <el-table-column label="仓库位置" prop="address"/>
      <!-- <el-table-column label="上级ID" align="center" prop="parentId" /> -->
      <!-- <el-table-column label="祖级列表" align="center" prop="ancestors" /> -->
      <!-- <el-table-column label="状态" align="center" prop="status" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_common_status" :value="scope.row.status" />
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="经度" align="center" prop="lng" />
      <el-table-column label="纬度" align="center" prop="lat" /> -->
      <!-- <el-table-column label="创建人" align="center" prop="createBy" /> -->
     <!-- <el-table-column label="删除标识" align="center" prop="delFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_del_flag" :value="scope.row.delFlag" />
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="260">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['wms:store:edit']">修改</el-button>
          <el-button type="text" icon="el-icon-plus" size="mini" @click="handleAdd(scope.row)"
              v-hasPermi="['wms:store:add']">新增</el-button>
          <el-button type="text" icon="el-icon-s-grid" size="mini" @click="handleArea(scope.row)"
              v-hasPermi="['wms:store:add']">库区({{scope.row.areas}})</el-button>
          <!-- <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['wms:store:remove']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

  </div>
</template>

<script>
  import {
    listStore,
    getStore,
    delStore,
    addStore,
    updateStore
  } from "@/api/wms/store";
  import {
    listSysArea
  } from "@/api/system/sysArea";
  import {
    listUser
  } from "@/api/system/user"
  import {
    treeselect
  } from "@/api/system/dept";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  export default {
    name: "Store",
    dicts: ['sys_common_status', 'sys_del_flag'],
    components: {
      Treeselect
    },
    data() {
      return {
        // 按钮loading
        buttonLoading: false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 是否展开，默认全部展开
        isExpandAll: true,
        // 重新渲染表格状态
        refreshTable: true,
        // 仓库表格数据
        storeList: [],
        provinceOptions: [],
        cityOptions: [],
        areaOptions: [],
        countyOptions: [],
        userOptions: [],
        deptOptions: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: undefined,
          deptId: undefined,
          userId: undefined,
          provinceCode: undefined,
          cityCode: undefined,
          areaCode: undefined,
          parentId: undefined,
          status: undefined,
          delFlag: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          name: [{
            required: true,
            message: "仓库名称不能为空",
            trigger: "blur"
          }],
          deptId: [{
            required: true,
            message: "所属机构不能为空",
            trigger: "blur"
          }],
          userId: [{
            required: true,
            message: "仓管员ID不能为空",
            trigger: "blur"
          }],
          provinceCode: [{
            required: true,
            message: "省代码不能为空",
            trigger: "blur"
          }],
          cityCode: [{
            required: true,
            message: "市代码不能为空",
            trigger: "blur"
          }],
          areaCode: [{
            required: true,
            message: "区县代码不能为空",
            trigger: "blur"
          }],
          address: [{
            required: true,
            message: "仓库位置不能为空",
            trigger: "blur"
          }],
        }
      };
    },
    watch: {
      'queryParams.deptId': {
        handler(newVal, oldVal) {
          this.getUsers();
        },
        deep: true,
        immediate: true
      }
    },
    created() {
      this.getList();
      this.getTreeselect();
      this.getAreaList(0, 1);
    },
    activated() {
        const time = this.$route.query.t;
        if (time != null && time != this.uniqueId) {
          this.uniqueId = time;
          this.queryParams.pageNum = Number(this.$route.query.pageNum);
          this.getList();
        }
    },
    methods: {
      /** 查询仓库列表 */
      getList() {
        this.loading = true;
        listStore(this.queryParams).then(response => {
          this.storeList = this.handleTree(response.data, "id");
          this.loading = false;
        });
      },
      handleArea(row){
        this.$router.push({
          path: '/wms/storeAreas/list',
          query: {
            pageNum: this.queryParams.pageNum,
            storeId: row.id
          }
        });
      },
      getAreaList(parentId, level) {
        listSysArea(Object({
          parentId: parentId,
          levels: level
        })).then(
          (response) => {
            if (level == 1) {
              this.provinceOptions = response.data;
            } else if (level == 2) {
              this.cityOptions = response.data;
            } else if (level == 3) {
              this.areaOptions = response.data;
            } else if (level == 4) {
              this.countyOptions = response.data;
            }
          }
        );
      },
      /** 查询部门下拉树结构 */
      getTreeselect() {
        treeselect().then((response) => {
          this.deptOptions = response.data;
        });
      }, //管理员信息
      getUsers() {
        listUser({
          'userType': 'sys_user',
          'deptId': this.queryParams.deptId
        }).then(response => {
          this.userOptions = response.rows
        })
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          name: undefined,
          deptId: undefined,
          userId: undefined,
          province: undefined,
          city: undefined,
          area: undefined,
          provinceCode: undefined,
          cityCode: undefined,
          areaCode: undefined,
          address: undefined,
          parentId: undefined,
          ancestors: undefined,
          status: undefined,
          lng: undefined,
          lat: undefined,
          createTime: undefined,
          createByu: undefined,
          updateTime: undefined,
          updateBy: undefined,
          delFlag: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd(row) {
        // this.reset();
        // this.open = true;
        // this.title = "添加仓库";
        let parentId = "";
        if (row != undefined) {
          parentId = row.id;
        }
        this.$router.push({
          path: '/wms/store/add',
          query: {
            pageNum: this.queryParams.pageNum,
            parentId: parentId
          }
        });
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        // this.loading = true;
        // this.reset();
        // const id = row.id || this.ids
        // getStore(id).then(response => {
        //   this.loading = false;
        //   this.form = response.data;
        //   this.open = true;
        //   this.title = "修改仓库";
        // });
        const id = row.id;
        this.$router.push({
          path: '/wms/store/edit/' + id,
          query: {
            pageNum: this.queryParams.pageNum
          }
        });
      },
      /** 展开/折叠操作 */
      toggleExpandAll() {
        this.refreshTable = false;
        this.isExpandAll = !this.isExpandAll;
        this.$nextTick(() => {
          this.refreshTable = true;
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除仓库编号为"' + ids + '"的数据项？').then(() => {
          this.loading = true;
          return delStore(ids);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).finally(() => {
          this.loading = false;
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('wms/store/export', {
          ...this.queryParams
        }, `store_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
